package br.ucb.cesbdao.model.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lombok.Getter;
import lombok.Setter;

import org.apache.log4j.Logger;

import br.ucb.cesbdao.beans.Frequencia;
import br.ucb.cesbdao.model.FrequenciaModel;
import br.ucb.cesbdao.persistencia.dao.FrequenciaDAO;
import br.ucb.cesbdao.persistencia.factory.FactoryConnection;
import br.ucb.cesbdao.persistencia.factory.FactoryDAO;
import br.ucb.cesbdao.servicos.Servicos;

public class FrequenciaModelImpl implements FrequenciaModel{

	private Logger logger = Servicos.carregaLog(FrequenciaModelImpl.class);
	@Getter @Setter
	private FrequenciaDAO dao;
	
	public FrequenciaModelImpl() {
	}
	
	@Override
	public void setFrequencia(Frequencia frequencia) {
		try{
			setDao(FactoryDAO.getDaoFrequencia(FactoryConnection.getInstance().criaConexao()));
			getDao().setFrequencia(frequencia);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
	}

	@Override
	public Frequencia getFrequenciabyChaveToday(Integer id) {
		try{
			setDao(FactoryDAO.getDaoFrequencia(FactoryConnection.getInstance().criaConexao()));
			return getDao().getFrequenciabyChaveToday(id);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public ArrayList<Frequencia> getFrequenciabyIdPessoa(Integer id) {
		try{
			setDao(FactoryDAO.getDaoFrequencia(FactoryConnection.getInstance().criaConexao()));
			return getDao().getFrequenciabyIdPessoa(id);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public ArrayList<Frequencia> getFrequenciabyIdPessoaPeriodo(Integer id, Date dataInicio, Date dataFim) {
		try{
			setDao(FactoryDAO.getDaoFrequencia(FactoryConnection.getInstance().criaConexao()));
			return getDao().getFrequenciabyIdPessoaPeriodo(id, dataInicio, dataFim);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}

	@Override
	public ArrayList<Frequencia> getFrequenciabyGrupo(Integer idGrupo, Date dataInicio, Date dataFim) {
		try{
			setDao(FactoryDAO.getDaoFrequencia(FactoryConnection.getInstance().criaConexao()));
			return getDao().getFrequenciabyGrupo(idGrupo, dataInicio, dataFim);
		} catch (ClassNotFoundException | SQLException e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		} finally {
			FactoryConnection.getInstance().closeConexao(getDao().getConexao());
		}
		return null;
	}
}
